﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;



[DefaultExecutionOrder(-1)]
[AddComponentMenu("UI/Behavior/PositionIndex")]
public class UIPositionIndex : MonoBehaviour
{

    public Vector3[] postions;
    private bool afterAwake = false;

    [Tooltip("改动的目标图标，不填写则自动获取默认值")]
    public Transform targetTransform;

    [SerializeField]
    private int _index;
    public int index
    {
        get
        {
            return _index;
        }
        set
        {
            if (value > postions.Length - 1) value = postions.Length - 1;
            if (value < 0) value = 0;
            UpdateFromIndex(value);
            _index = value;
        }
    }

#if UNITY_EDITOR
    private void OnValidate()
    {
        if (postions == null || postions.Length <= 0) return;
        if (_index > postions.Length - 1) _index = postions.Length - 1;
        if (_index < 0) _index = 0;
        targetTransform.localPosition = postions[_index];
    }

#endif

    private void Awake()
    {
        this.afterAwake = true;
        if(targetTransform == null){
            targetTransform = this.transform;
        }
    }


    private void UpdateFromIndex(int value)
    {
        if (afterAwake == true)
        {
            targetTransform.localPosition = postions[value];
        }
    }
}
